/**
 * JAVACC DEMO 1.0
 */
package com.apache.portal.common.template.helper;

import com.apache.portal.common.template.FreemarkerHelper;
import com.apache.portal.common.util.FileOperate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description:  生成Action实现类
 *
 * @author dyhou 创建时间：2016-9-30
 */
public class ActionTempManager extends SupperTemplateManager {

    public boolean createAction(Map<String, Object> param) {
        boolean mark = false;
        List<Map<String, Object>> list = (List<Map<String, Object>>) param.get("sortList");
        String jarPath = String.valueOf(param.get("packPath"));
        String prefix = String.valueOf(param.get("projectName")) + "/src/main/";
        String xmlPath = String.valueOf(param.get("filePath")) + "/" + prefix + "java/" + jarPath
                .replace(".", "/") + "/controller/";
        //		String sysName = jarPath.substring(jarPath.lastIndexOf(".") + 1);//com.iussoft.utr中的utr
        String sysName = String.valueOf(param.get("sysCode"));
        FileOperate.getInstance().newCreateFolder(xmlPath);
        String restfullClassName =
                "Restfull" + FileOperate.getInstance().toUpperFirst(sysName) + "Action";
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("restfullClassName", restfullClassName);
        data.put("packPath", jarPath);
        data.put("reqName", sysName);
        try {
            String result = FreemarkerHelper.instance()
                    .getTemplateStr(data, "controller/RestfullAction.inc");
            FileOperate.getInstance().newCreateFile(xmlPath + restfullClassName + ".java", result);
            result = FreemarkerHelper.instance().getTemplateStr(data, "controller/SuperAction.inc");
            FileOperate.getInstance().newCreateFile(xmlPath + "UnitySupperAction.java", result);
            for (Map<String, Object> map : list) {
                String reqObjName = FileOperate.getInstance()
                        .toCamelNamed(String.valueOf(map.get("sortCode")));
                data.put("reqObjName", reqObjName);
                data.put("sort", map);
                result = FreemarkerHelper.instance()
                        .getTemplateStr(data, "controller/ObjAction.inc");
                FileOperate.getInstance()
                        .newCreateFile(xmlPath + String.valueOf(map.get("objName")) + "Action.java",
                                result);
            }
            mark = true;
        } catch (Exception e) {
            log.error("生成action失败", e);
        }
        return mark;
    }
}
